package me.sdevil507.systemAssist.systemLog.service.impl;

import com.google.common.collect.Lists;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import me.sdevil507.systemAssist.systemLog.mapper.SystemOperLogMapper;
import me.sdevil507.systemAssist.systemLog.po.QSystemOperLogPo;
import me.sdevil507.systemAssist.systemLog.po.SystemOperLogPo;
import me.sdevil507.systemAssist.systemLog.repository.SystemOperLogRepository;
import me.sdevil507.systemAssist.systemLog.service.SystemOperLogService;
import me.sdevil507.systemAssist.systemLog.dto.SystemOperLogParamsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SystemOperLogServiceImpl implements SystemOperLogService {

    @Autowired
    private SystemOperLogRepository systemOperLogrepository;

    @Autowired
    private SystemOperLogMapper systemOperLogMapper;

    @Override
    public Page<SystemOperLogPo> readAllByPage(SystemOperLogParamsDto searchParamsDto, PageParamsDTO pageParamsDto, SortParamsDTO sortParamsDto) {
        QSystemOperLogPo qSystemOperLogPo = QSystemOperLogPo.systemOperLogPo;

        // 开启谓语查询条件
        Predicate predicate = qSystemOperLogPo.id.isNotNull();

        // 获取查询参数
        String module = searchParamsDto.getOperModule();
        if (!StringUtils.isEmpty(module)) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.operModule.eq(module));
        }
        String operTerminal = searchParamsDto.getOperTerminal();
        if (!StringUtils.isEmpty(operTerminal)) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.operTerminal.eq(operTerminal));
        }
        String operClassify = searchParamsDto.getOperClassify();
        if (!StringUtils.isEmpty(operClassify)) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.operClassify.eq(operClassify));
        }
        Long operOrgId = searchParamsDto.getOperOrgId();
        if (operOrgId != null) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.operOrgId.eq(operOrgId));
        }
        Date startTime = searchParamsDto.getStartTime();
        Date endTime = searchParamsDto.getEndTime();
        if (null != startTime) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.createTime.goe(startTime));
        }
        if (null != endTime) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.createTime.loe(endTime));
        }

        // 排序
        Sort sort = sortParamsDto.getSort();
        if (null == sort) {
            sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"), new Sort.Order(Sort.Direction.DESC, "createTime"));
        }

        // 执行返回
        Pageable pageable = pageParamsDto.getPageableWithSort(sort);

        return systemOperLogrepository.findAll(predicate, pageable);
    }

    @Override
    public List<SystemOperLogPo> readAll(SystemOperLogParamsDto searchParamsDto, PageParamsDTO pageDto, SortParamsDTO sortDto) {
        QSystemOperLogPo qSystemOperLogPo = QSystemOperLogPo.systemOperLogPo;

        // 开启谓语查询条件
        Predicate predicate = qSystemOperLogPo.id.isNotNull();

        // 获取查询参数pid
        String module = searchParamsDto.getOperModule();
        if (!StringUtils.isEmpty(module)) {
            predicate = ExpressionUtils.and(predicate, qSystemOperLogPo.operModule.eq(module));
        }

        // 排序
        Sort sort = sortDto.getSort();
        if (null == sort) {
            sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"), new Sort.Order(Sort.Direction.DESC, "uploadTime"));
        }
        return Lists.newArrayList(systemOperLogrepository.findAll(predicate, sort).iterator());
    }

    @Override
    public void add(SystemOperLogPo operlog) {
        systemOperLogrepository.save(operlog);
    }

    @Override
    public SystemOperLogPo readById(Long id) {
        return systemOperLogrepository.findById(id).orElse(null);
    }

}
